<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var lengthOfLongestSubstring = function (s) {
            //定义一个新数组
            var set = [];
            //i遍历数组，j删除重复字符串
            var i = 0,
                j = 0,
            //得到最大长度
                maxlength = 0;
            if (s.length === 0) {
                return 0;
            }
            //遍历数组
            for (i; i < s.length; i++) {
            //当新数组中没有 目标字符时
                if (!set.includes(s[i])) {
                    //添加字符
                    set.push(s[i])
                    //取最长字串长度
                    maxlength = Math.max(maxlength,set.length)
                } 
                
                else {
                    //当新数组中存在 目标字符时
                    while (set.includes(s[i])) {
                        //删除所有 目标字符前的字符
                        set.shift()
                        j++;
                    }
                    // 添加 目标字符
                    set.push(s[i])
                }

            }
            return maxlength;
        };
        var s = "abcabcbb";
        console.log(lengthOfLongestSubstring(s))
    </script>
</body>

</html>